import {createRouter, createWebHashHistory} from 'vue-router'

import ShowSchedule from '../components/ShowSchedule.vue'
import Login from '../components/Login.vue'
import Register from '../components/Register.vue'

import pinia from '../pinia'
import {defineUser} from '../store/userStore'

// 不知道这个pinia参数什么时候需要给，什么时候不需要
let sysUser = defineUser(pinia)

const router = createRouter({
    history:createWebHashHistory(),
    routes:[
        {
            path:"/",
            // 重定向
            redirect:"/showSchedule"
            // component:ShowSchedule
        },
        {
            path:"/showSchedule",
            component:ShowSchedule
        },
        {
            path:"/login",
            component:Login
        },
        {
            path:"/register",
            component:Register
        }
    ]
})

// 配置路由的前置守卫，在登录状态下才可以访问网站内容
router.beforeEach((to,from,next)=>{
    if(to.path=="/showSchedule"){
        if(sysUser.username==''){
            alert("用户未登录")
            next("/login")
        }else{
            next()
        }
    }else{
        next()
    }
})

export default router